#include<iostream>

// 单链表
struct ListNode
{
    int val;
    ListNode *next;
    ListNode():val(0), next(nullptr){}
    ListNode(int x):val(x), next(nullptr){}
    ListNode(int x, ListNode *next):val(x), next(next){}
};

// 方法二：简化双指针法
ListNode *ReverseList(ListNode *head) {
    ListNode *tmp;
    ListNode *current_node = head;
    ListNode *current_pre_node = nullptr;
    while (current_node != nullptr)
    {
        tmp = current_node->next;
        current_node->next = current_pre_node;
        current_pre_node = current_node;
        current_node = tmp;
    }
    return current_pre_node;
}

void OutPutListNodeVal(ListNode *head)
{
    std::cout<<"[";
    ListNode *cur_node = head;
    while (cur_node != nullptr)
    {
        std::cout<<cur_node->val;
        cur_node = cur_node->next;
        if (cur_node != nullptr)
        {
            std::cout<<", ";
        }
    }
    std::cout<<"]."<<std::endl;
}

void TestCase1()
{
    //case 1
    // 输入：head = [1,2,3,4,5]
    // 输出：[5,4,3,2,1]
    ListNode *case1_node0 = new ListNode(1);
    ListNode *case1_node1 = new ListNode(2);
    ListNode *case1_node2 = new ListNode(3);
    ListNode *case1_node3 = new ListNode(4);
    ListNode *case1_node4 = new ListNode(5);
    case1_node0->next = case1_node1;
    case1_node1->next = case1_node2;
    case1_node2->next = case1_node3;
    case1_node3->next = case1_node4;

    ListNode *head = ReverseList(case1_node0);
    // output
    OutPutListNodeVal(head);
}

void TestCase2()
{
    //case 2
    // 输入：head = [1,2]
    // 输出：[2,1]
    ListNode *case2_node0 = new ListNode(1);
    ListNode *case2_node1 = new ListNode(2);
    case2_node0->next = case2_node1;

    ListNode *head = ReverseList(case2_node0);
    // output
    OutPutListNodeVal(head);
}

void TestCase3()
{
    //case 3
    // 输入：head = [1]
    // 输出：[1]
    ListNode *case3_node0 = new ListNode(1);

    ListNode *head = ReverseList(case3_node0);
    // output
    OutPutListNodeVal(head);
}

int main()
{
    TestCase1(); 
    TestCase2();   
    TestCase3();
    return 0;
}